package com.yusvn.scm.goods.shoudongfenhuo.service;


import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.yitter.idgen.YitIdHelper;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yusvn.scm.admin.service.LoginService;
import com.yusvn.scm.common.BaseService;
import com.yusvn.scm.common.constant.Const;
import com.yusvn.scm.common.kit.RetKit;

import java.util.ArrayList;
import java.util.List;

public class ScmShouDongFenHuoService extends BaseService {

    @Inject
    LoginService loginService;

    /**
     * 查询需要手动分货的商品
     * @param kv
     * @return
     */
    public Page<Record> getPageSdfhsp(Kv kv){
        // 手动分货动态字段
        kv.set(selectSdfhPz());
        String zc = Db.use(Const.SCMDB).queryStr("select id from defstore where store = (select pz1 from scm_pz where bh = 'scm_sdfh_zc') and cid = ? ", getCid());
        kv.set("zc_id",zc);
        kv.set("cid",getCid() );
        kv.set("goodsimg", getGoodsimg()); //商品图片地址
        Page<Record> records = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.getSdfhsp",kv).paginate(getPage(kv),getLimit(kv));
        return records;
    }


    /**
     * 获取商品动态属性对应表
     * @return
     */
    public List<Record> selectAllAttrib() {
        List<Record> records = Db.use(Const.SCMDB).find("select a.pzmx1 as dtzd, b.id as id, b.attribdef as attribdef, b.attribdefname " +
                "from (" +
                "  select * from scm_pzmx where mid=(select id from SCM_PZ where bh='scm_sdfh_dtzd') " +
                ") a " +
                "inner join (" +
                "  select id, 'attrib'||to_number(attribdef) as dtzderp, attribdef, attribdefname from defattribdef where isallow=1 and cid=? " +
                ") b on a.pzmx2=b.dtzderp "
                + "", getCid());

        return records;
    }

    /**
     * 获取某个商品属性
     * @param kv
     * @return
     */
    public List<Record> selctAttribById(Kv kv) {


        if (Integer.parseInt(kv.get("sfpp").toString()) == 1){
            List<Record> records = Db.use(Const.SCMDB).find("select * from defattrib where attribdefid = ? and  cid = ? and sfpp = 1", kv.get("attr"), getCid());
            return records;
        }

        List<Record> records = Db.use(Const.SCMDB).find("select * from defattrib where attribdefid = ? and  cid = ? order by id desc", kv.get("attr"), getCid());

        return records;
    }

    /**
     * 根据spid, ysid 查询库存
     * @param kv
     * @return
     */
    public List<Record> selectQtycan(Kv kv){

        kv.set("cid",getCid());

        String zc = Db.use(Const.SCMDB).queryStr("select id from defstore where store = (select pz1 from scm_pz where bh = 'scm_sdfh_zc') and cid = ? ", getCid());
        kv.set("zc_id",zc);
        List<Record> records = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.selectQtycan",kv).find();

        return records;
    }

    /**
     * 手动分货表三查询
     */
    public List<Record> selectSdfhCz(Kv kv){
        if (!"wu".equals(kv.get("dataTime").toString())){
            List<String> strData = (List<String>) kv.get("dataTime");
            kv.set("data1",strData.get(0));
            kv.set("data2",strData.get(1));
        }
        kv.set("cid",getCid());
        System.out.println("kv"+kv);
        List<Record> records = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.selectSdfhCz",kv).find();

        return records;
    }

    /**
     * 查询本次分货明细
     */
    public List<Record> selectBcsdfhmx(Kv kv){
        List<Record> records = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.selectBcsdfhmx",kv).find();

        return records;
    }

    /**
     * 新增或修改分货数量
     */
    public RetKit insertScmSdfhjg(Kv kv){
        String zc = Db.use(Const.SCMDB).queryStr("select id from defstore where store = (select pz1 from scm_pz where bh = 'scm_sdfh_zc') and cid = ? ", getCid());
        Integer integer1 = Db.use(Const.SCMDB).queryInt(
        " select nvl(kc.qtycan,0)  - nvl(fh.fhsl,0) as fhsl" +
                "  from defskuview st" +
                "   left join (" +
                "   select skuid, qtycan, storeid from accstock" +
                "   where storeid = ?" +
                "   )kc on kc.skuid = st.id" +
                "  left join (" +
                "        select skuid, sum(fhsl) as fhsl from scm_sdfh_sdfhjg" +
                "        group by skuid" +
                "  )fh on fh.skuid = st.id" +
                " where st.cid = ? and st.styleid = ? and st.clrid = ? and st.sizeid = ?"
        , zc, getCid(), kv.get("spid"), kv.get("ysid"), kv.get("cmid"));
        if ((integer1 + Integer.parseInt(kv.get("xgqsl").toString()) - Integer.parseInt(kv.get("fhsl").toString())) < 0){
            return RetKit.fail("库存不足");
        }
        String skuid = Db.use(Const.SCMDB).queryStr("select id from defskuview " +
                "where cid = ? and styleid = ? and clrid = ? and sizeid = ? "
                , getCid(), kv.get("spid"), kv.get("ysid"), kv.get("cmid"));
        Integer integer = Db.use(Const.SCMDB).queryInt("select count(*) " +
                        " from scm_sdfh_sdfhjg " +
                        " where spid = ? and ysid = ? and cmid = ? and mdid = ? and skuid = ?"
                , kv.get("spid"), kv.get("ysid"), kv.get("cmid"), kv.get("mdid"), skuid);
        if (integer <= 0) {
            Db.use(Const.SCMDB).update("insert into scm_sdfh_sdfhjg(id, mdid, skuid, spid, ysid, cmid, fhsl, czyid)" +
                    " values(?, ?, ?, ?, ?, ?, ?, ?)", YitIdHelper.nextId(), kv.get("mdid"), skuid, kv.get("spid")
                    , kv.get("ysid"), kv.get("cmid"), kv.get("fhsl"), kv.get("userid"));
                return RetKit.ok("新增成功");
        } else {
            Db.use(Const.SCMDB).update("update  scm_sdfh_sdfhjg " +
                    " set fhsl = ? where mdid = ? and skuid = ?", kv.get("fhsl"),  kv.get("mdid"), skuid);
            return RetKit.ok("修改成功");
        }
    }

    /**
     * 根据门店查询分货明细
     */
    public List<Record> sdfhBymdid(Kv kv){
        //获取原erp用户id
        String erpUserid = loginService.getErpUserId(kv.get("userid"), getCid());
        kv.set("erpUserid", erpUserid);
        List<Record> records = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.sdfhBymdid",kv).find();

        return records;
    }

    /**
     * 查询店仓级别
     */
    public List<Record> selectDefstoreType(Kv kv){

        kv.set("cid", getCid());

        List<Record> records = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.selectDefstoreType", kv).find();
        return records;
    }

    /**
     * 查询店仓性质
     */
    public List<Record> selectDefstoreFlag(Kv kv){

        kv.set("cid", getCid());

        List<Record> records = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.selectDefstoreFlag", kv).find();
        return records;
    }

    /**
     * 根据条件查询门店
     */
    public List<Record> selectMdList(Kv kv){
        kv.set("cid", getCid());

        String zc = Db.use(Const.SCMDB).queryStr("select id from defstore where store = (select pz1 from scm_pz where bh = 'scm_sdfh_zc') and cid = ? ", getCid());
        kv.set("zc_id",zc);

        List<Record> records = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.selectMdList", kv).find();

        return records;
    }

    /**
     * 整手新增或者修改分货数量
     */
    public RetKit zsUpdateOrinsertSdfh(Kv kv){
        Object sizeList = kv.get("sizeList");
        if (sizeList instanceof List<?>) {
            Boolean kcSfcz = false;
            List<String> kcbzCm = new ArrayList<>();
            List<?> myList = (List<?>) sizeList;
            // 现在 myList 包含了 sizeList 中的数据，你可以对它进行操作
            String zc = Db.use(Const.SCMDB).queryStr("select id from defstore where store = (select pz1 from scm_pz where bh = 'scm_sdfh_zc') and cid = ? ", getCid());
            for (Object item : myList) {
                Record first = Db.use(Const.SCMDB).findFirst("select COALESCE(NVL(kc.qtycan, 0), 0) - COALESCE(fh.fhsl, 0) as fhsl " +
                                " from defskuview st  " +
                                " left join (" +
                                " select skuid, qtycan, storeid from accstock " +
                                " where storeid = ?" +
                                " )kc on kc.skuid = st.id  " +
                                " left join (        " +
                                " select skuid, sum(fhsl) as fhsl " +
                                " from scm_sdfh_sdfhjg        " +
                                " group by skuid  )fh on fh.skuid = st.id " +
                                " where st.cid = ? and st.styleid = ? and st.clrid = ? and st.sizeid = ?"
                        , zc, getCid(), kv.get("spid"), kv.get("ysid"), item);
                Integer integer = Db.use(Const.SCMDB).queryInt("select nvl(fhsl,0) from scm_sdfh_sdfhjg where spid = ? and mdid = ? and ysid = ? and cmid = ?"
                        , kv.get("spid"), kv.get("mdid"), kv.get("ysid"), item);
                if (integer == null){
                    integer = 0;
                }
                Integer fhslSp = 0;
                if(first != null){
                    fhslSp = Integer.parseInt(first.get("fhsl").toString());
                }
                if ( (fhslSp + integer - Integer.parseInt(kv.get("fhsl").toString())) < 0) {
                    kcSfcz = true;
                    // 查询cmmc
                    String s = Db.use(Const.SCMDB).queryStr("select sizename from defskuview where cid = ? and sizeid = ? and rownum <= 1", getCid(), item.toString());
                    if (!kcbzCm.contains(s)){
                        kcbzCm.add(s);
                    }
                }else{
                    Integer xiugai = Db.use(Const.SCMDB).queryInt("select count(*) " +
                                    " from scm_sdfh_sdfhjg " +
                                    " where spid = ? and ysid = ? and cmid = ? and mdid = ? "
                            , kv.get("spid"), kv.get("ysid"), item, kv.get("mdid"));
                    if (xiugai > 0 ){
                        Db.use(Const.SCMDB).update("update scm_sdfh_sdfhjg set fhsl = ? " +
                                        " where mdid = ? and spid = ? and ysid = ? and cmid = ? "
                                , kv.get("fhsl"), kv.get("mdid"), kv.get("spid"), kv.get("ysid"), item);
                    }else {
                        String skuid = Db.use(Const.SCMDB).queryStr("select id from defskuview " +
                                        "where cid = ? and styleid = ? and clrid = ? and sizeid = ? "
                                , getCid(), kv.get("spid"), kv.get("ysid"), item);
                        Db.use(Const.SCMDB).update("insert into scm_sdfh_sdfhjg(id, mdid, skuid, spid, ysid, cmid, fhsl, czyid) " +
                                        " values(?, ?, ?, ?, ?, ?, ?, ?)"
                                , YitIdHelper.nextId(),kv.get("mdid"), skuid, kv.get("spid"), kv.get("ysid")
                                , item, kv.get("fhsl"), kv.get("userid"));
                    }
                }
            }
            if (kcSfcz){
                String kcbzString = "";
                for (String s : kcbzCm) {
                    kcbzString += "," + s;
                }
                return RetKit.fail("库存不足"+kcbzString);
            }
            return RetKit.ok("操作成功");
        }

      return RetKit.fail("操作失败");
    }

    /**
     * 手动分货生成单据
     */
    public RetKit sdfhScdj(Kv kv){
        //账套id
        String cid = getCid();
        kv.set("cid", cid);
        String userid=kv.getStr("userid");

        //获取原erp用户id
        String erpUserid = loginService.getErpUserId(kv.get("userid"), cid);
        if(StrKit.isBlank(erpUserid)) {
            return RetKit.fail("未获取到有效的ERP账号,请到用户管理中设置");
        }
        kv.set("erpUserid", erpUserid);

        //获取从藏得所属经销商
        String zcssjxs = Db.use(Const.SCMDB).queryStr("select BUYERID from defstore where store = (select pz1 from scm_pz where bh = 'scm_sdfh_zc') and cid = ? ", cid);

        try {
            //先删除之前临时表用户勾选的id
            Db.use(Const.SCMDB).update("delete from scm_base_ids " +
                    "where cid=? and userid=? and lx='scm_sdfj_sc' ", cid, userid);
            //存入用户勾选的id到临时表
            JSONArray idsArr = JSON.parseArray(kv.getStr("mdids"));
            Object[][] paras = new Object[idsArr.size()][3];
            Object[][] mdidList = new Object[idsArr.size()][1];
            for(int i=0;i<idsArr.size();i++) {
                paras[i][0] = idsArr.get(i);
                mdidList[i][0] = idsArr.get(i);
                paras[i][1] = cid;
                paras[i][2] = userid;
            }
            Db.use(Const.SCMDB).batch("insert into scm_base_ids(id, cid, lx, userid) values(?, ?, 'scm_sdfh_sc', ?)", paras, 500);

            Db.use(Const.SCMDB).update("CALL proc_scm_sdfh_sc(?, ?, ?, ? )" , cid, userid, erpUserid, zcssjxs);

            Db.use(Const.SCMDB).batch("delete scm_sdfh_sdfhjg where mdid = ? ", mdidList, 500);
            return RetKit.ok("生成单据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return RetKit.fail("生成单据失败:"+e.getMessage() );
        }
    }

    /**
     * 查询总仓存在的商品
     */
    public List<Record> getZcSp(Kv kv){
        String zc = Db.use(Const.SCMDB).queryStr("select id from defstore where store = (select pz1 from scm_pz where bh = 'scm_sdfh_zc') and cid = ? ", getCid());
        kv.set("zc_id",zc);
        kv.set("cid",getCid());

        List<Record> records = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.getZcSp", kv).find();

        return records;
    }

    /**
     * 获取商品图片和供应商
     */
    public Record getPstpGys(Kv kv){
        kv.set("cid", getCid()); //账套id
        //获取商品图片地址
        kv.set("goodsimg", getGoodsimg());
        return Db.use(Const.SCMDB).template("scmBuhuoZbbh.getPstpGys", kv).findFirst();
    }

    /**
     * 删除分货结果
     */
    public RetKit deleteTableClick(Kv kv){

        JSONArray idsArr = JSON.parseArray(kv.getStr("mdids"));
        Object[][] paras = new Object[idsArr.size()][2];
        for(int i=0;i<idsArr.size();i++) {
            paras[i][0] = idsArr.get(i);
            paras[i][1] = kv.get("userid");
        }
      int[] cntArr =   Db.use(Const.SCMDB).batch("delete scm_sdfh_sdfhjg where mdid = ? and czyid = ? ", paras,500);

        //统计成功行数
        int cntOk = 0;
        for(int i=0; i<cntArr.length; i++) {
            if(cntArr[i]==-2 || cntArr[i]==1) { //oracle返回-2、sqlserver返回1，表示成功
                cntOk++;
            }
        }

        if(cntArr.length>0) {
            return RetKit.ok("成功删除 "+cntOk+" 条数据");
        }

        return RetKit.fail("批量删除失败");
    }


    /**
     * 查询已选择的门店名称
     */
    public List<Record> selectYxzmd(Kv kv){

        List<Record> records = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.selectYxzmd",kv).find();

        return records;
    }

    /**
     * 新增已选择的门店
     */
    public RetKit insertYxzmd(Kv kv){

        // 判断门店是否存在
        Integer integer = Db.use(Const.SCMDB).queryInt("select count(*) from scm_sdfh_yxzmd where mdid = ? and userid = ? "
                , kv.get("mdid"), kv.get("userid"));
        if (integer <= 0){
            Db.use(Const.SCMDB).update("insert into scm_sdfh_yxzmd(id, mdid, mdmc, userid) values (?, ?, ?, ?)"
            , YitIdHelper.nextId(), kv.get("mdid"), kv.get("mdmc"), kv.get("userid"));
            return RetKit.ok();
        }
        return RetKit.fail();
    }

    /**
     * 删除已选择门店
     */
    public RetKit deleteYxzmd(Kv kv){
        int update = Db.use(Const.SCMDB).update("delete from scm_sdfh_yxzmd where id = ? and userid = ?"
                , kv.get("id"), kv.get("userid"));
        if (update > 0) {
            return RetKit.ok();
        }
        return RetKit.fail();
    }


    /**
     *批量修改或新增分货
     */
    public RetKit plInsertOrUpdate(Kv kv){
        //查询选择的门店
        kv.set("cid",getCid());
        kv.set("id",kv.get("spid"));
        String zc = Db.use(Const.SCMDB).queryStr("select id from defstore where store = (select pz1 from scm_pz where bh = 'scm_sdfh_zc') and cid = ? ", getCid());
        kv.set("zc_id",zc);
        List<Record> mdList = Db.use(Const.SCMDB).find("select mdid from scm_sdfh_yxzmd where userid = ? ", kv.get("userid"));
        Object[][] paras = new Object[mdList.size()][4];
        for(int i=0;i<mdList.size();i++) {
            paras[i][0] = mdList.get(i).get("mdid");
            paras[i][1] = kv.get("userid");
            paras[i][2] = kv.get("spid");
            paras[i][3] = kv.get("ysid");
        }
        // 删除批量已经配的数据
        Db.use(Const.SCMDB).batch("delete from scm_sdfh_sdfhjg where mdid = ? and czyid = ? and spid = ? and ysid = ? "
                , paras, 100);

        //查询出款式有的尺码

        List<Record> records = Db.use(Const.SCMDB).template("scmBase.getProductSizes", kv).find();
        Boolean cmmcYc = false;
        // 记录库存不足的尺码
        List<String> cmBzList = new ArrayList<>();
        // 循环门店
        for (Record record : mdList) {
            // 循环尺码
            for (int i = 0; i < records.size(); i++) {
                // 查询门店可配库存
                kv.set("cmid",records.get(i).get("cmid"));
                Record first = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.selectQtycanCmid", kv).findFirst();
                if(first==null) {
                	continue;
                }
                if (Integer.parseInt(first.get("qtycan").toString()) - Integer.parseInt(first.get("fhsl").toString())
                        - Integer.parseInt(kv.get("sl").toString()) >= 0){
                    // 查询sku
                    String skuid = Db.use(Const.SCMDB).queryStr("select id from defskuview where cid = ? and styleid = ? and clrid = ? and sizeid = ? ",
                            getCid(), kv.get("spid"), kv.get("ysid"), records.get(i).get("cmid"));
                    Db.use(Const.SCMDB).update("insert into scm_sdfh_sdfhjg(id, mdid, skuid, spid, ysid, cmid, fhsl, czyid)" +
                            " values(?, ?, ?, ?, ?, ?, ?, ?)",
                            YitIdHelper.nextId(), record.get("mdid"), skuid, kv.get("spid"), kv.get("ysid")
                            , records.get(i).get("cmid"), kv.get("sl"), kv.get("userid"));
                }else {
                    cmmcYc = true;
                     if(!cmBzList.contains(records.get(i).get("cmmc").toString())){
                         cmBzList.add(records.get(i).get("cmmc").toString());
                     }
                }
            }
        }
        if (cmmcYc){
            String pjString = "";
            for (String s : cmBzList) {
                pjString += "," + s;
            }
            return RetKit.fail("库存不足"+pjString);
        }

        return RetKit.ok("操作成功");
    }

    /**
     * 根据销售数量分货
     */
    public RetKit xsfhInsert(Kv kv){
        kv.set("cid",getCid());
        kv.set("id",kv.get("spid"));
        String zc = Db.use(Const.SCMDB).queryStr("select id from defstore where store = (select pz1 from scm_pz where bh = 'scm_sdfh_zc') and cid = ? ", getCid());
        kv.set("zc_id",zc);
        //查询选择的门店
        List<Record> mdList = Db.use(Const.SCMDB).find("select mdid from scm_sdfh_yxzmd where userid = ? ", kv.get("userid"));
        Object[][] paras = new Object[mdList.size()][4];
        for(int i=0;i<mdList.size();i++) {
            paras[i][0] = mdList.get(i).get("mdid");
            paras[i][1] = kv.get("userid");
            paras[i][2] = kv.get("spid");
            paras[i][3] = kv.get("ysid");
        }
        // 删除批量已经配的数据
        Db.use(Const.SCMDB).batch("delete from scm_sdfh_sdfhjg where mdid = ? and czyid = ? and spid = ? and ysid = ? "
                , paras, 100);

        //查询出款式有的尺码
        Boolean kcSfcz = false;
        //库存不足
        List<String> kcbzList = new ArrayList<>();
        List<Record> records = Db.use(Const.SCMDB).template("scmBase.getProductSizes", kv).find();
        // 循环门店
        for (Record record : mdList) {
            // 循环尺码
            for (int i = 0; i < records.size(); i++) {
                // 查询sku
                String skuid = Db.use(Const.SCMDB).queryStr("select id from defskuview where cid = ? and styleid = ? and clrid = ? and sizeid = ? ",
                        getCid(), kv.get("spid"), kv.get("ysid"), records.get(i).get("cmid"));
                //查询门店sku销售数量
                Integer xssl = Db.use(Const.SCMDB).queryInt("select nvl(retail,0) from ACCSTOCK where cid = ? and skuid = ? and storeid = ? "
                        , getCid(), skuid, record.get("mdid"));
                if (xssl == null){
                    xssl = 0;
                }
                //判断xssl是否大于0
                if (xssl > 0){
                    // 查询门店可配库存
                    kv.set("cmid",records.get(i).get("cmid"));
                    Record first = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.selectQtycanCmid", kv).findFirst();
                    if (Integer.parseInt(first.get("qtycan").toString()) - Integer.parseInt(first.get("fhsl").toString())
                            - xssl >= 0) {
                        //新增分货数据
                        Db.use(Const.SCMDB).update("insert into scm_sdfh_sdfhjg(id, mdid, skuid, spid, ysid, cmid, fhsl, czyid)" +
                                        " values(?, ?, ?, ?, ?, ?, ?, ?)"
                                , YitIdHelper.nextId(), record.get("mdid"), skuid, kv.get("spid"), kv.get("ysid")
                                , records.get(i).get("cmid"), xssl, kv.get("userid"));
                    }else {
                        kcSfcz = true;
                        if (!kcbzList.contains(records.get(i).get("cmid").toString())){
                            String s = Db.use(Const.SCMDB).queryStr("select sizename from defskuview where cid = ? and sizeid = ? and rownum <= 1"
                                    , getCid(), records.get(i).get("cmid").toString());
                            kcbzList.add(s);
                        }
                    }
                }
            }
        }
        if (kcSfcz){
            String bzkcCm = "";
            for (String s : kcbzList) {

                bzkcCm += "," + s;
            }
            return RetKit.fail("库存不足"+bzkcCm);
        }
        return RetKit.ok("操作成功");
    }

    public List<Record> dlhzButton(Kv kv){

        kv.set(selectPzFh()); //商品属性动态字段
        //获取原erp用户id
        String erpUserid = loginService.getErpUserId(kv.get("userid"), getCid());
        kv.set("erpUserid", erpUserid);
        List<Record> records = new ArrayList<>();
            records = Db.use(Const.SCMDB).template("scmShouDongFenHuoSdfh.dlhzDl",kv).find();
        return records;
    }

    /**
     *
     */
    public RetKit plinsertMd(Kv kv){
        String userid = kv.get("userid").toString();
        Object listObject = kv.get("mdidList");
        if (listObject instanceof List) {
            List<?> rawList = (List<?>) listObject;

            // Check if the list contains elements of type JSONObject
            if (!rawList.isEmpty() && rawList.get(0) instanceof com.alibaba.fastjson.JSONObject) {
                List<com.alibaba.fastjson.JSONObject> jsonObjectList = (List<com.alibaba.fastjson.JSONObject>) rawList;

                Object[][] paras = new Object[jsonObjectList.size()][4];
                for (int i = 0; i < jsonObjectList.size(); i++) {
                    com.alibaba.fastjson.JSONObject jsonObject = jsonObjectList.get(i);

                    paras[i][0] = YitIdHelper.nextId();
                    paras[i][1] = jsonObject.get("id");
                    paras[i][2] = jsonObject.get("name");
                    paras[i][3] = userid;
                }

                Db.use(Const.SCMDB).batch("insert into scm_sdfh_yxzmd(id, mdid, mdmc, userid) values(?, ?, ?, ?)", paras, 100);
                return RetKit.ok();
            }
        }
        return RetKit.fail();
    }

    public RetKit pldeleteMd(Kv kv){
        Db.use(Const.SCMDB).update("delete from scm_sdfh_yxzmd where userid = ? ", kv.get("userid"));
        return RetKit.ok();
    }
}
